﻿using UnityEngine;
using System.Collections;

public class portico : BM_LevelObject {

	public	player_script 	player;
	public	Camera 			playerCamera;
	public	Transform 		andy;
	public	Transform 		mamma;
	public	Transform 		mamma_tex;
	public	Transform 		porta;
	public 	GUIText			theEnd;

	private bool startAnimation = false;
	private bool endAnimation = false;
	private bool startMovement = false;
	private bool fadeOut = false;
	private Vector3 prevVec;
	private float time;
	public float travelTime = 1.5f;

	public void OnTriggerEnter(Collider other) {
		if(other.transform == andy || (other.transform == player.transform && player.GetLastObject() == BM_BaseToy.Toy.TOY_ANDY)) {
		
		//if(true){

			// END GAME!!!
			m_rMessageHandler.SendMessageToAllButMe(new BM_BaseMessage(MessageID.MSGID_STOP_INTERACTION, MessageHandler));
			m_rMessageHandler.SendMessageToAllButMe(new BM_QuestActivationMessage(MessageID.MSGID_QUEST_DRAGONSLAYER_TO_HOME, MessageHandler, true));
			Debug.Log("END GAME");

			// HEY! urlo mamma
			audio.Play();

			mamma_tex.renderer.enabled = true;

			prevVec = player.transform.position;
			startAnimation = true;
		}
	}

	public void LateUpdate(){
		
		if(startAnimation) {
			var p1 = player.transform.position;
			var p2 = p1 - player.transform.forward*10;
			var p4 = mamma.position;
			var p3 = p4 - Vector3.right*10;

			Vector3 vec = GetBezierPoint(time, p1, p2, p3, p4);
			playerCamera.transform.position = vec;
			playerCamera.transform.forward = Vector3.Lerp( player.transform.forward, mamma.forward, time);
			prevVec = vec;
			time += Time.deltaTime/travelTime;

			if(time > 1.0f) {
				time = 0.0f;
				startAnimation = false;
				endAnimation = true;
				audio.Play();
			}
		}
		if(endAnimation) {
			time += Time.deltaTime;
			if(time > 1.0f) {
				time = 0.0f;
				endAnimation = false;
			/*	player.transform.LookAt(porta);
				playerCamera.transform.position = player.transform.position;
				playerCamera.transform.forward = player.transform.forward;
				startMovement = true;
			*/	
				fadeOut = true;
				theEnd.enabled = true;
			}
		}
		if(startMovement) {
			time += Time.deltaTime / 1000.0f;
			player.transform.parent.transform.position = Vector3.Lerp(player.transform.parent.transform.position, porta.position, time);
		}
	}

	public Texture2D fadeTexture;
	public float fadeSpeed = 0.1f;
	private int drawDepth = 1000;
	
	private float alpha = 0.0f;
	private int fadeDir = -1;
	
	public void OnGUI() {
		if(fadeOut) {
			alpha -= fadeDir * fadeSpeed * Time.deltaTime;
			alpha = Mathf.Clamp01(alpha);
			
			GUI.color  = new Color(1, 1, 1, alpha);
			GUI.depth = drawDepth;
			GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);

			//GUI.Label(new Rect(
			

		}
	}

	private Vector3 GetBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2 , Vector3 p3)
	{
		float u  = 1.0f - t;
		float tt  = t * t;
		float uu  = u * u;
		float uuu  = uu * u;
		float ttt  = tt * t;
		
		Vector3 p = uuu * p0; //first term
		p += 3 * uu * t * p1; //second term
		p += 3 * u * tt * p2; //third term
		p += ttt * p3; //fourth term
		
		return p;
	}
}
